package com.swing.gui;


import com.swing.assets.Itool;
import com.swing.assets.Iurl;
import com.swing.gui.component.MiniPopup;
import com.swing.gui.component.MiniTray;
import com.swing.gui.pages.GamePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Java Swing JFrame 双重校验（单例模式）
 *
 * @author Yuang, Gongming
 * @date 2022-01-13 13:24
 */
public class APP extends JFrame {

    private static APP app;

    private APP() {
        //  禁止 new APP();
    }

    public static APP init() {
        if (app == null) {
            synchronized (APP.class) {
                if (app == null) {
                    app = new APP();
                    try {
                        template();
                    } catch (MalformedURLException e) {
                        //  模板构建失败 ignored
                    }
                    listener();
                }
            }
        }
        return app;
    }

    /**
     * 初始化模板
     */
    private static void template() throws MalformedURLException {
        ImageIcon icon = new ImageIcon(new URL(Iurl.INIT));
        icon.setImage(icon.getImage().getScaledInstance(20, 20, Image.SCALE_AREA_AVERAGING));
        app.setIconImage(icon.getImage());
        app.setTitle("休闲（开发中）");
        app.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        app.setSize(Itool.WIN_WIDTH / 2, Itool.WIN_HEIGHT / 2);
        app.setLocationRelativeTo(null);
        app.setVisible(true);
    }

    /**
     * 初始化事件
     */
    private static void listener() {
        app.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (MouseEvent.BUTTON3 == e.getButton()) {
                    System.out.println("鼠标右击：" + System.currentTimeMillis());
                    MiniPopup.init();
                }
                if (MouseEvent.BUTTON1 == e.getButton() && 3 < e.getClickCount()) {
                    JOptionPane.showMessageDialog(app, "敬请期待……");
                }
//                if(MouseEvent.BUTTON1 == e.getButton()){
//                    APP.init().add(new GamePanel());
//                }
            }
        });

        app.addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowGainedFocus(WindowEvent e) {
                System.out.println("获取主窗口焦点" + System.currentTimeMillis());
            }

            @Override
            public void windowLostFocus(WindowEvent e) {
                System.out.println("主窗口失焦" + System.currentTimeMillis());
            }

        });

        app.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //  todo - 窗口关闭时最小化到系统托盘？
                System.out.println("窗体关闭：" + System.currentTimeMillis());
                System.exit(0);
            }

            @Override
            public void windowIconified(WindowEvent e) {
                System.out.println("窗体图标化：" + System.currentTimeMillis());
                app.setVisible(false);
                Settings.init().setVisible(false);
                //  添加系统托盘
                MiniTray.init();
            }


        });

    }

}
